package com.yxj.Practice4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class Server {
    public static void main(String[] args) throws IOException {
        //1.
        ServerSocket ss = new ServerSocket(10000);

        //2.等待客户来连接
        Socket socket = ss.accept();

        ///3.读取数据并保存到本地文件中
        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        String name = UUID.randomUUID().toString().replace("-","");

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\Ccode\\java-se\\Mysocketnet\\"+name+".png"));
        int len;
        byte[] bytes = new byte[1024];
        while((len = bis.read(bytes)) != -1) {
            bos.write(bytes,0,len);
        }

        //4.回写数据
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("上传成功");
        bw.newLine();
        bw.flush();

        //5.
        socket.close();
        ss.close();
    }
}
